<?php

namespace addons\hlapp\controller;

use addons\hlapp\dto\Result;
use addons\hlapp\model\HlappMember;
use addons\hlapp\model\HlappPart;
use addons\hlapp\model\HlappProject;
use addons\hlapp\model\HlappProjectEvaluate;
use addons\hlapp\model\HlappProjectStatus;
use think\Db;

class Member extends Common
{
    public $adminLogin = true;//注意：因为这是后台业务菜单所以需要管理员登录操作的
    public $pageSize = 10;


    /**
     * 分页
     */
    public function pagelist()
    {
        $m = new HlappMember();
        $name = $this->request->param("name");
        $status = $this->request->param('status');
        $phone = trim($this->request->param('phone'));
        if ($status == '1') {
            $m = $m->where('openId is not null');
        } else if ($status == '0') {
            $m = $m->where('openId  is null');
        }
        if ($phone){
            $m = $m->where('phone',$phone);
        }
        $list = $m->where('name', 'like', '%' . $name . '%')
            ->order('id', 'desc')->with('parts')
            ->paginate($this->pageSize, false, ['query' => request()->param()]);
        $page = request()->param('page');
        $page || ($page = 1);

        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('name', $name);
        $this->assign('phone', $phone);
        return $this->fetch();
    }
//=========================================================

    /**
     * 分页
     */
    public function memberList()
    {
        $this->paginationPage();
        return $this->fetch();
    }


    /**
     * 添加人员页面
     */
    public function memberAdd()
    {
        $this->assign('part', HlappPart::getParts());
        return $this->fetch();
    }

    /*
     * 修改页面
     */
    public function memberUpdate()
    {
        $id = $_GET['id'];
        $m = new  HlappMember();
        $member = $m->where('id', $id)->with('parts')->find();
        $part = HlappPart::getParts();
        $this->assign('member', $member->getData());
        $this->assign('part', $part);
        return $this->fetch();
    }


    /**
     * 添加
     */
    public function addMember()
    {
        $m = new HlappMember();
        $member = $m->where('phone', $_POST['phone'])->find();
        if ($member && !empty($member)) {
            $r = Result::getError("手机号已经存在！");
        } else {
            $id = $m->saveOrUpdata($_POST);
            $r = Result::getOk($id);
        }
        return $r;
    }

    /*
     * 更新
     */
    public function updateMember()
    {
        $id = (new HlappMember())->saveOrUpdata($_POST);
        $r = Result::getOk($id);
        return $r;
    }

    /**
     * 删除成员
     * @return string
     */
    public function deleteMember()
    {
        $id = $_POST['id'];
        $r = HlappMember::get($id)->delete();
        if ($r) {
            return Result::getOk();
        } else {
            return Result::getError();
        }
    }

    /**
     * 分页
     * @return string
     */
    public function pagination()
    {
        $m = new HlappMember();
        $r = $m->getPage($m->whereSql(), 'parts');
        return Result::getOk($r);
    }

    /**
     * 总部人员列表页
     */
    public function centerMemberlist(){
        $m = new HlappMember();
        $memberList = $m->where('partId','in',HlappPart::$CENTERTYPE)->with('parts')->select();
        $this->assign('list', $memberList);
        return $this->fetch();
    }

}